package com.teacher.hong.servlet.clazz;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.teacher.hong.entity.Clazz;
import com.teacher.hong.service.IClazzService;
import com.teacher.hong.service.impl.ClazzServiceImpl;
import com.teacher.hong.utils.TokenUtils;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 班级列表
 */
//@WebServlet("/clazz")
public class ClazzListServlet extends HttpServlet {

    IClazzService clazzService;

    @Override
    public void init() throws ServletException {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        clazzService = context.getBean("clazzService", ClazzServiceImpl.class);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        List<Clazz> clazzes = clazzService.list();

        JSONObject object = new JSONObject(true);
        object.put("state", 200);

        object.put("value", JSON.toJSON(clazzes));
        object.put("timestamp", LocalDateTime.now());

        resp.setStatus(200);

        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(object.toJSONString());

    }
}
